/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanCarte.java
 *
 * Created on 10 janv. 2012, 15:00:29
 */
package InterfaceGraphique.commonPannel;

import InterfaceGraphique.FenJeuEntrer;
import cartes.Carte;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.TransferHandler;

/**
 *
 * @author arthur.hoareau
 */
public class PanCarte extends javax.swing.JPanel implements MouseListener {
    
    Carte carte;
    BufferedImage img;
    private PanImageGlass ip;
    private JFrame parent;
    private PanListeCarte container;
    private TransferHandler th;
    private int width             =120;
    private int height            =186;
    private boolean selectable    =false;
    private boolean selected      =false;
    private boolean grisee        =false;
    private boolean imageEnCours  =false;
    
    /** Creates new form PanCarte */
    public PanCarte() {
        this.carte = null;        
        initComponents();          
    }
    
    /**
     * Constructeur pour les miniatures des cartes
     * @param name
     * @param parent
     * @throws URISyntaxException 
     */
    public PanCarte(Carte c, JFrame parent, PanListeCarte container, boolean selectable) {
        try {
            this.parent = parent;
            this.carte   = c;
            this.selectable = selectable;
            this.container = container;
            //this.setBorder(BorderFactory.createLineBorder(Color.BLUE));   
            //this.setBorderPainted(true);
            setPreferredSize(new Dimension(this.width, this.height));
            
            
            // ON CHOPPE LES PUTAINS DE CARTES QUI BUGUENT
            System.out.println(c.toString());
            
            this.img = ImageIO.read(getClass().getResource(c.getCheminImage()));
            addMouseListener(this);  
            this.setOpaque(false);             
        } catch (IOException ie) {
            System.out.println("Error:" + ie.getMessage());
        }
    }

    
    /**
     * Constructeur pour la carte en cours
     * @param name
     * @param parent
     * @param width
     * @param height
     * @throws URISyntaxException 
     */
    public PanCarte(Carte c, JFrame parent, int width, int height, PanListeCarte container, boolean selectable) throws URISyntaxException {
        this(c, parent, container, selectable);
        this.width  = width;
        this.height = height;
        setPreferredSize(new Dimension(this.width, this.height)); 
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setPreferredSize(new java.awt.Dimension(120, 186));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    
    /**
     * Setter du boolean qui permet de voir si le click sur la carte est autorisé
     * @param selectable 
     */
    public void setClick_allowed(boolean click_allowed) {
        this.selectable = click_allowed;       
        this.repaint();
    }

    
    /**
     * Setter du boolean qui permet de grisée une carte non clicable
     * @param grisee 
     */
    public void setGrisee(boolean grisee) {
        this.grisee = grisee;
        this.repaint();
    }
    
    
    /**
     * getter du nom, de l'id de la carte
     * @return 
     */
    public String getImageName(){
        return this.carte.getNom();
    }   
    
    @Override
    public void paint(Graphics g) {      
        super.printBorder(g);
        g.drawImage( this.img, 5, 5,this.width-10,this.height-10, null);
        if(grisee && !imageEnCours){
            g.setColor(new Color(255, 255, 255,155));
            g.fillRect(0, 0, this.width, height);
        }
    }

    
    /**
     * Evenement click
     * @param e 
     */
    @Override
    public void mouseClicked(MouseEvent e) {        
        if(selectable){
            this.setSelected(!this.isSelected());
            if( this.selected == true ) {
                this.container.setSelected(this);
            }
            else {
                this.container.setSelected(null);
            }
        }    
        this.repaint();
    }
    
    /**
     * Non Necessaire, mais on peut pas supprimer car interface
     * @param e 
     */
    @Override
    public void mousePressed(MouseEvent e) {
        
    }
    
    /**
     * Non Necessaire, mais on peut pas supprimer car interface
     * @param e 
     */
    @Override
    public void mouseReleased(MouseEvent e) {
        
    }

    
    /**
     * Evenement survol de la carte
     * On gere le zoom de la carte
     * @param e 
     */
    @Override
    public void mouseEntered(MouseEvent e) {   
        Point containP = this.container.getLocation();
        Point p=this.getLocation(); 
        
         ip=new PanImageGlass(this.img,p.getX() + containP.getX(), p.getY() + containP.getY() ); 
         parent.setGlassPane(ip); 
         ip.setOpaque(false);
         ip.repaint();
         if(!grisee){
            ip.setVisible(true);
         }
    }    
    
    /**
     * ON dezoom
     * @param e 
     */
    @Override
    public void mouseExited(MouseEvent e) {       
        ip.setVisible(false);        
    }   

    public BufferedImage getImg() {
        return img;
    }

    boolean isSelected() {
        return this.selected;
    }

    void setSelected(boolean b) {
        this.selected = b;
        
        if( b ) {
            this.setBorder(BorderFactory.createLineBorder(Color.RED,5));            
        }
        else {
            this.setBorder(null);            
        }
    }
    
    
}
